#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
typedef int DataType;
typedef struct LNode
{
	DataType data;
	struct LNode* next;
}LNode,*LinkList;
//创建表头
LinkList createList()
{
	LinkList phead = (LinkList)malloc(sizeof(LNode));
	phead->next = NULL;
	return phead;
}
//创建结点
LinkList createNode(DataType data)
{
	LinkList newNode = (LinkList)malloc(sizeof(LNode));
	newNode->data = data;
	newNode->next = NULL;
	return newNode;
}
//头插法
void insertNodeByHead(LinkList phead, DataType data)
{
	LinkList newNode=createNode(data);
	newNode->next = phead->next;
	phead->next = newNode;
}
//尾插法
void insertNodeByTail(LinkList phead, DataType data)
{
	LinkList p = phead;
	while (p->next)
	{
		p = p->next;
	}

	LinkList newNode = createNode(data);
	p->next = newNode;
}
//打印
void printList(LinkList phead)
{
	LinkList pMove = phead->next;
	while (pMove)
	{
		printf("%d ", pMove->data);
		pMove = pMove->next;
		\
	}
	printf("\n");
}
//删除
void deleteElem(LinkList phead, int p)
{
	LinkList temp = phead;
	int i = 0;
	//找到删除结点的上一结点，即第p-1个结点
	while (i < p - 1)
	{
		temp = temp->next;
		i++;
	}
	LinkList q = temp->next;//定义一个q指针指向被删除结点
	LinkList w = q->next;
	temp->next = w;
	free(q);				//释放q所指结点的内存空间 
}
//查
void searchInfoByData(LinkList phead,int i)
{
	LinkList pMove = phead;
	if (pMove->next == NULL)
		printf("出现错误无法查找");
	int count = 0;
	for (count = 0; count < i; count++)
	{
		pMove = pMove->next;
	}
	printf("%d\n", pMove->data);
}
//改
void changeList(LinkList phead, int i,DataType data)
{
	LinkList pMove = phead;
	if (pMove->next == NULL)
		printf("出现错误无法更改");
	int count = 0;
	for (count = 0; count < i; count++)
	{
		pMove = pMove->next;
	}
	pMove->data = data;
}
int main()
{

	LinkList phead = createList();

	insertNodeByHead(phead, 4);
	insertNodeByHead(phead, 7);
	insertNodeByTail(phead, 5);
	insertNodeByTail(phead, 9);
	printList(phead);
	deleteElem(phead, 2);
	printList(phead);
	searchInfoByData(phead, 1);
	changeList(phead, 2, 100);
	printList(phead);
	return 0;
}
